﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows;
using Rifts.Models.Definitions;

namespace Rifts.Creator
{
	/// <summary>
	/// Interaction logic for ClassSkills.xaml
	/// </summary>
	public partial class ClassSkills : Page
	{
		public ClassSkills()
		{
			InitializeComponent();
			DataContextChanged += ClassSkills_DataContextChanged;
		}

		void ClassSkills_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			Reset();
			TypedDataContext.ResetAction = Reset;
		}

		public CreatorViewModel TypedDataContext
		{
			get { return (CreatorViewModel)DataContext; }
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			var skillDefinition = (SkillDefinitionViewModel)((FrameworkElement)sender).DataContext;
			TypedDataContext.ApplyOccSkill(skillDefinition);
			if (TypedDataContext.OccSkills.All(s => !s.MetRequirements))
				TypedDataContext.CanMoveNext = true;
		}

		private void Reset()
		{
			TypedDataContext.Character.RejectChanges();
			TypedDataContext.OccSkills.Clear();
			TypedDataContext.OccSkills.AddRange(TypedDataContext.SelectedClass.OccSkills());
			TypedDataContext.OccSkillPicks = TypedDataContext.Character.MaxOccSkillsFromIQ(TypedDataContext.SelectedClass.OccSkillPicks, TypedDataContext.Random);
			TypedDataContext.UpdateOccSkills();
			TypedDataContext.CanMoveNext = false;
		}
	}
}
